package com.yun.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yun.mapper.ArticleMapper;
import com.yun.pojo.Article;
import com.yun.unit.Code;
import com.yun.unit.PageUnit;
import com.yun.unit.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService{
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public R selByUserId(PageUnit pageUnit) {
        System.out.println("pageUnit = " + pageUnit);
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        List<Article> articles = articleMapper.selectAllById(pageUnit.getId());
        //System.out.println("articles = " + articles);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
        if (articlePageInfo != null){
            return new R(Code.get_ok,"查询成功",articlePageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }
    //假删文章
    @Override
    public R updById(Integer id) {
        int i = articleMapper.upById(id);
        if (i>0){
            return new R(Code.delete_ok,"删除成功");
        }
        return new R(Code.delete_err,"删除失败");
    }
    //查看详情
    @Override
    public R selById(Integer id) {
        Article article = articleMapper.selById(id);
        if (article != null){
            return  new R(Code.get_ok,"查询成功",article);
        }
        return new R(Code.get_err,"查询失败");
    }
    //根据文章标题查询文章
    @Override
    public R selByName(PageUnit pageUnit) {
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        List<Article> articles = articleMapper.selByName(pageUnit.getKeywords(), pageUnit.getId());
        if (articles != null){
            PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
            return new R(Code.get_ok,"查询成功",articlePageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }


    //查询被假删掉的文章
    @Override
    public R selByDel(PageUnit pageUnit) {
        PageHelper.startPage(pageUnit.getPagenum(),pageUnit.getPagesize());
        List<Article> articles = articleMapper.selByDel(pageUnit.getId());
        if (articles != null){
            PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
            return new R(Code.get_ok,"查询成功",articlePageInfo);
        }
        return new R(Code.get_err,"查询失败");
    }

    @Override
    public R updByDel(Integer id, Integer del) {
        int i = articleMapper.updByIdD(id, del);
        if (i>0){
            return new R(Code.delete_ok,"删除成功");
        }
        return new R(Code.delete_err,"删除失败");
    }

    //新增文章
    @Override
    public R insAll(Article article) {
        int i = articleMapper.insertAll(article);
        if (i>0){
            return new R(Code.save_ok,"新增成功");
        }
        return new R(Code.save_err,"新增失败");
    }

    //修改文章内容
    @Override
    public R updAll(Article article) {
        int i = articleMapper.updAll(article);
        if (i>0){
            return new R(Code.update_ok,"修改成功");
        }
        return new R(Code.update_err,"修改失败");
    }
}
